package com.mulanweb.api.module.controller;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.mulanweb.api.module.model.*;
import com.mulanweb.db.common.Constant;
import com.mulanweb.db.common.SQLGenerateHelper;
import com.mulanweb.handler.wechat.UserHandler;
import com.mulanweb.validator.annotation.ValidateFile;
import com.mulanweb.validator.annotation.ValidateForm;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2016/9/26.
 */
@ValidateFile
public class CollectionController extends BaseController {

    @ValidateForm
    public void saveOrQuery(){
        String openId = getSessionAttr("openId");//"oULNBwOvt_D2A_8se1bQOQ06ky0w";//"oULNBwM6ak3KaPmHcIW2WdB_9kQg";//
        if (UserHandler.userNotNull(openId)) {
            Integer typeId = getParaToInt("typeId");
            Long targetId = getParaToLong("targetId");
            String typeTag = getPara("typeTag");
            User user = User.dao.findFirst("SELECT * FROM `user` WHERE open_id=?",openId);
            Collection collection = Collection.dao.findFirst("SELECT * FROM collection WHERE type_id =? AND target_id =? AND user_id =?",typeId,targetId,user.getId());

            //如果存在收藏则删除，不存在则保存收藏或删除收藏
            if("query".equals(typeTag)){//表示查询，不执行保存或删除收藏操作
                if(StrKit.notNull(collection)){
                        returnJson(1,"1");//表示存在此内容的收藏，
                        return;
                }else{
                    returnJson(1,"2");//表示不存在此内容的收藏
                    return;
                }
            }else{
                if(StrKit.notNull(collection)){
                    if(collection.delete()){
                        returnJson(1,"2");
                    }else{
                        returnFailureJson();
                    }
                    return;
                }
            }
            collection = new Collection();
            collection.setTypeId(typeId);
            collection.setTargetId(targetId);
            collection.setUserId(user.getId());
            collection.setCreateTime(new Date());
            if(typeId == 23){
                Ticket ticket = Ticket.dao.findById(targetId);
                if((!StrKit.notNull(ticket)) || ticket.getDeleteTime() !=null){
                    returnJson(0,"内容不存在或已下架");
                    return;
                }
            }else if(typeId ==24){
                collection.setTypeId(typeId);
                Chartered chartered =Chartered.dao.findById(targetId);
                if(!StrKit.notNull(chartered) || chartered.getDeleteTime() != null){
                    returnJson(0,"内容不存在或已下架");
                    return;
                }
            }else{
                returnFailureJson();
                return;
            }
            if(collection.save()){
                returnJson(1,"1");
            }else{
                returnFailureJson();
            }
        }else{
            returnJson(0,Constant.PromptMessage.NOT_EXIST_USER);
        }
    }


}
